<?php

include "global.php"; // Get Configuration
getsettings();

error_reporting(5);
####### Checking the website Status ############ //
if ($website_active != '1') {
eval("dooutput(\"".gettemplate("closed_page")."\");");
exit;
}

$records_per_page = 25;
$max_pages = 10;

list($member_id,$email,$sess) = check_session("volunteer");
list($fname,$lname,$title,$company,$industry,$address,$volunteer_details) = $DB_site->query_first("SELECT fname,lname,title,company,industry,address,details FROM volunteers WHERE id='$member_id' AND email='$email'");

if ($_GET["doaction"] == "view_email")
{
	$rid = $_GET["rid"];
	$email_id = $_GET["eid"];
	$contents = file_get_contents("$website_email_path/$rid");
	echo $contents;
	exit;
}

if ($_GET["doaction"] == "mark_complete" AND $_GET["tid"] != "")
{
	$tid = $_GET["tid"];
	list($check) = $DB_site->query_first("SELECT count(*) FROM tars_emails WHERE tar_id='$tid' AND volunteer='$member_id'");
	if ($check > 0)
	{
		$result = $DB_site->query("UPDATE tars SET tar_status='complete',email_status='Scheduled',volunteer='$member_id' WHERE id='$tid'");
		$result = $DB_site->query("UPDATE tars_emails SET email_status='complete' WHERE tar_id='$tid' AND volunteer='$member_id'");
		$msg = "Request Successful";
	}
}

if ($_GET["doaction"] == "change_in_progress" AND $_GET["tid"] != "")
{
	$tid = $_GET["tid"];
	list($check) = $DB_site->query_first("SELECT count(*) FROM tars_emails WHERE tar_id='$tid' AND volunteer='$member_id'");
	if ($check > 0)
	{
		$result = $DB_site->query("UPDATE tars SET tar_status='pending',email_status='In-Progress',volunteer='0' WHERE id='$tid'");
		$result = $DB_site->query("UPDATE tars_emails SET email_status='pending' WHERE tar_id='$tid' AND volunteer='$member_id'");
		$msg = "Request Successful";
	}
}


$page = $_GET["page"];
$field_name = trim($_GET["field_name"]);
$field_value = trim($_GET["field_value"]);
$field_value_all = trim($_GET["field_value_all"]);
$sortby = $_GET["sortby"];
$orderby = $_GET["orderby"];

// Process Detailed Search
if ($field_name != '' AND $field_value != '')
{
  $field_names = explode(",",$field_name);
  $field_values = explode(",",$field_value);
  $jump_query = "";
  for ($i = 0; $i < sizeof($field_names); $i++)
  {
    if ($field_names[$i] != "" AND $field_values[$i] != "")
    {
      $name = $field_names[$i];
      $value = urldecode($field_values[$i]);
      $jump_query .= "$name LIKE '%$value%' AND ";
      ${$name . "_option"} = "selected";
	  }
	}
	$jump_query .= "1=1";
	$sort_string .= "field_name=$field_name&field_value=$field_value";
}

// Process Detailed Search
if ($field_name != '' AND $field_value_all != '')
{
  $field_names = explode(",",$field_name);
  $jump_query = "(";
  for ($i = 0; $i < sizeof($field_names); $i++)
  {
    if ($field_names[$i] != "")
    {
      $name = $field_names[$i];
      $value = urldecode($field_value_all);
      $jump_query .= "$name LIKE '%$value%' OR ";
      ${$name . "_option"} = "selected";
	  }
	}
	$jump_query .= "1=0) ";
	$sort_string .= "field_name=$field_name&field_value_all=$field_value_all";
}
//echo $jump_query;

$sort_array = array();
$query_array = array();
if ($sortby == "") {$sortby = "ID";}
if ($orderby == "") {$orderby = "DESC";}
$sort_query = "&sortby=$sortby&orderby=$orderby&";
$sortby = "$sortby $orderby";

if (trim($sort_string) == "") {
	$sort_string = "?";
}
else {
	$sort_string = "?".$sort_string."&";
}

if ($jump_query != '') {$listquery = "WHERE $jump_query AND volunteer='$member_id' ";}
else {$listquery = "WHERE volunteer='$member_id'  ";}

$field = trim($field);
$text = trim($text);

//echo $listquery;

// Get the Pages
list($total_count) = $DB_site->query_first("SELECT count(*) FROM tars_emails $listquery");
if ($total_count > $records_per_page)
{
	$pages = $total_count/$records_per_page;
	if (gettype($pages) == "double")
	{
		settype($pages,integer);
		$pages++;
	}
  // Show the First Page
	if (!isset($page))
	{
		$page=1;
		$string_pages = ".Top $records_per_page are shown.You can Go directly to PAGES: [";
		$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=1'>&lt;&lt;</a>&nbsp;&nbsp;";

		for($count=1;$count<=$pages;$count++)
		{
		  // Show only max pages
		  if ($count == $max_pages)
		  {
        $string_pages.="...";
        continue;
      }
      if ($count > $max_pages) {continue;}

			if ($count != $page) {
				$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=$count'>";
			}
			$string_pages.="$count&nbsp;&nbsp;";
			if ($count != $page) {
			$string_pages.="</a>";
			}
		}
		$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=2'>&gt;</a>&nbsp;&nbsp;";
		$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=$pages'>&gt;&gt;</a>";
		if (!isset($sortby)) {$sortby = "id ASC";}
		$result = $DB_site->query("SELECT `id`,`tar_id`, `teacher_id`, `county_name`, `district_name`, `school_name`, `school_city`, `school_zip`, `subject`, `category`, `category_name`, `best_times`, `months`, `details`, `grades`, `students`, `submit_time`, `email_message`, `email_status`, `email_dated`, `volunteer`, `rating`, `complete_time`,`rand_id` FROM tars_emails $listquery ORDER BY $sortby LIMIT $records_per_page");
		$string_pages.=" ]";
	}
	else // Show the Requested Page
	{
		$string_pages = " You can go directly to PAGES: [";
		$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=1'>&lt;&lt;</a>&nbsp;&nbsp;";
		if ($page > 1) {
			$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=".($page-1)."'>&lt;</a>&nbsp;&nbsp;";
		}

		for($count=1;$count<=$pages;$count++)
		{
		  // Show only max pages on either side of selected page
		  if ( (($page - $count) == $max_pages/2) || (($count - $page) == $max_pages/2) )
		  {
        $string_pages.="...";
        continue;
      }
		  if ( (($page - $count) > $max_pages/2) || (($count - $page) > $max_pages/2) )
		  {
        continue;
      }

			if ($count != $page) {
				$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=$count'>";
			}
			$string_pages.="$count&nbsp;&nbsp;";
			if ($count != $page) {
				$string_pages.="</a>";
			}
		}
		if ($page < $pages) {
			$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=".($page+1)."'>&gt;</a>&nbsp;&nbsp;";
		}
		$string_pages.="<a href='$website_volunteers_overview".$sort_string. $sort_query . "page=$pages'>&gt;&gt;</a> ";
		$start_limit=($page-1)*$records_per_page;
		if (!isset($sortby)) {$sortby = "id ASC";}
		$result = $DB_site->query("SELECT `id`,`tar_id`, `teacher_id`, `county_name`, `district_name`, `school_name`, `school_city`, `school_zip`, `subject`, `category`, `category_name`, `best_times`, `months`, `details`, `grades`, `students`, `submit_time`, `email_message`, `email_status`, `email_dated`, `volunteer`, `rating`, `complete_time`,`rand_id` FROM tars_emails $listquery ORDER by $sortby LIMIT $start_limit,$records_per_page");
		$string_pages.=" ]";
	}
}
else
{
	$string_pages = "";
	if (!isset($sortby)) {$sortby = "id ASC";}
	$result = $DB_site->query("SELECT `id`,`tar_id`, `teacher_id`, `county_name`, `district_name`, `school_name`, `school_city`, `school_zip`, 
	`subject`, `category`, `category_name`, `best_times`, `months`, `details`, `grades`, `students`, `submit_time`, `email_message`, `email_status`, `email_dated`, `volunteer`, `rating`, `complete_time`,`rand_id` FROM tars_emails $listquery ORDER BY $sortby");
}

$string1 = "";
$count1 = 0;
$color = 0;

// Flip The Order
if ($orderby == "ASC") {$orderby = "DESC";}
elseif ($orderby == "DESC") {$orderby = "ASC";}

if ($total_count == "") $total_count = "0";
$current_subscribers = "";

$current_color = 0;

$current_requests = "";
//$tquery = $DB_site->query("SELECT id,county_name,district_name,school_name,category_name,months,best_times,details,grades,students FROM tars  ORDER BY ID DESC");
while (list($email_id,$tar_id,$teacher_id,$county_name,$district_name,$school_name,$school_city,$school_zip,$subject,$category,$category_name,$best_times,$months,$details,
			$grades,$students,$submit_time,$email_message,$email_status,$email_dated,$volunteer,$rating,$complete_time,$rand_id) = $DB_site->fetch_array($result))
{
	$flag = 1;
	list($teacher_fname,$teacher_lname,$school_id,$teacher_email) = $DB_site->query_first("SELECT fname,lname,school,email FROM teachers WHERE id='$teacher_id'");
	list($responses) = $DB_site->query_first("SELECT count(*) FROM tars_emails WHERE tar_id='$tar_id'");
	list($tar_status) = $DB_site->query_first("SELECT tar_status FROM tars WHERE id='$tar_id'");
	
	if ($tar_status == "pending") {$tar_status = "In-Progress"; $tar_action = "<a href='$website_volunteers_contacts?doaction=mark_complete&tid=$tar_id'>Mark as Complete</a>";}
	if ($tar_status == "complete") {$tar_status = "Complete"; $tar_action = "<a href='$website_volunteers_contacts?doaction=change_in_progress&tid=$tar_id'>Change to In-Progress</a>";}
	
	list($school_address) = $DB_site->query_first("SELECT address FROM schools WHERE id='$school_id'");
	$best_times = ($best_times == "") ? "-" : $best_times;
	$grades = ($grades == "0") ? "K" : "$grades";
	$current_requests.= "<tr class=\"trclass$current_color\">
						<td class=\"results\">$teacher_lname, $teacher_fname &nbsp;</td>
						<td class=\"results\">$email_dated &nbsp;</td>
						<td class=\"results\">$school_name<br /><span class=\"small_text black1\">$school_address</span><br /><span class=\"small_text black1\">$school_city<br />$school_zip</span></td>
						<td class=\"results\">$subject</td>
						<td class=\"results\">$category_name</td>
						<td class=\"results\"><span class=\"small_text  strong\">Grade $grades:</span> <span class=\"small_text  black1\">$details</span></td>
						<td class=\"results\">$months<br /><span class=\"small_text  black1\">$best_times</span></td>
						<td class=\"results\">$students</td>
						<td class=\"results\"><a href=\"#\" onClick=\"MM_openBrWindow('$website_volunteers_contacts?doaction=view_email&rid=$rand_id&eid=$email_id','some','scrollbars=yes,resizable=yes,width=500,height=500');return false;\">$email_id</a></td>
						<td class=\"results\">$tar_status / $tar_action</td>
						</tr>";
	$current_color = ($current_color == "0") ? "1" : "0";
}

$category_dropdown = "<select name='field_value'><option value=''></option>";
$cquery = $DB_site->query("SELECT id,category_name FROM categories WHERE is_active='Y' ORDER BY display_order ASC");
while (list($cid,$catname) = $DB_site->fetch_array($cquery))
{
  if ($field_name == "category_name" AND $field_value == $catname)
  {
    $category_dropdown .= "<option value='$catname' selected>$catname</option>";
    $field_value = ""; 
  }
  else
    $category_dropdown .= "<option value='$catname'>$catname</option>";
}
$category_dropdown .= "</select>";



eval("dooutput(\"".gettemplate("volunteers_contacts")."\");");
exit;

?>
